﻿using MedicalManagementAPP.AccountModule;
using MedicalManagementAPP.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MedicalManagementAPP.Forms
{
    public partial class RegisterRecord : Form
    {
        Person patient;
        string selectedDepartmentID;
        string selectedDocID;
        PatientForm patientForm;
        public RegisterRecord(Person patient,PatientForm patientForm)
        {
            InitializeComponent();
            this.patient = patient;
            skinEngine1.SkinFile = @"irisskin2\Wave.ssk";
            this.patientForm = patientForm;
        }

        private void RegisterRecord_Load(object sender, EventArgs e)
        {
            string sql = "select DepartmentID,DepartmentName from Department where DepartmentName<>'管理部门'";
            DataTable data = Connection.SqlQuery(sql);
            foreach(DataRow row in data.Rows)
            {
                departmentComboBox.Items.Add(row[0].ToString()+" "+ row[1].ToString());
            }
        }

        private void departmentComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedDepartmentID = departmentComboBox.SelectedItem.ToString().Substring(0,4);
            refreshDocComboBox();
        }
        private void refreshDocComboBox()
        {
            docComboBox.Items.Clear();
            string sql = "select WorkerID,WorkerName,WorkerType from Workers where DepartmentID=@Did";
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@Did",selectedDepartmentID)
            };
            DataTable data = Connection.SqlQuery(sql,parameters);
            foreach (DataRow row in data.Rows)
            {
                docComboBox.Items.Add(row[0].ToString() + " " + row[1].ToString() + " " + row[2].ToString());
            }
        }

        private void docComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedDocID = docComboBox.SelectedItem.ToString().Split(' ')[0];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 提交按钮
            if(selectedDocID==null || selectedDepartmentID == null) { MessageBox.Show("请先选择");return; }
            RecordSetManager.CreateRecordSetByID(selectedDocID, patient.PId);
            MessageBox.Show("创建成功，按下确定以关闭挂号窗口");
            patientForm.InitRecordsListView();
            this.Close();
        }
    }
}
